<?php
/**
 * User: hjd-yao
 * Date: 2019/4/30
 * Time: 18:28
 */

namespace Frontend\Http\Middleware;


use App\Http\Middleware\Backend\Middleware;
use Frontend\Models\UserBaseModel;
use Illuminate\Http\Request;
use Closure;

class CheckUserLogin extends Middleware
{
    public function handle(Request $request, Closure $next)
    {
        if(!UserBaseModel::init()->isLogin()){
            if($request->ajax()){
                return response()->json(['status'=>0,'msg'=>'请登录','url'=>route('f_Login_login')]);
            }else{
                return redirect()->route('f_Login_login');
            }
        }

        if(!UserBaseModel::init()->isOtherLogin()){
            if($request->ajax()){
                return response()->json(['status'=>0,'msg'=>'您的账户已在其他设备上登陆','url'=>route('f_Login_login')]);
            }else{
                return redirect()->route('f_Login_login');
            }
        }
        $this->loginData();
        return $next($request);
    }

    public function loginData()
    {
        view()->share('user_info',UserBaseModel::init()->loginInfo());
        \request()->offsetSet('user_id',UserBaseModel::init()->loginInfo('member_id'));

    }


}